<?php

namespace common\models;
use himiklab\sitemap\behaviors\SitemapBehavior;
use yii\helpers\Url;
use Yii;

/**
 * This is the model class for table "catelog".
 *
 * @property integer $id
 * @property integer $parent_id
 * @property string $title
 * @property string $slug
 * @property string $description
 * @property string $iconfont
 * @property integer $enabled
 * @property integer $created_at
 * @property integer $updated_at
 *
 * @property Note[] $notes
 */
class Catelog extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'catelog';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['parent_id', 'enabled', 'created_at', 'updated_at'], 'integer'],
            [['title'], 'required'],
            [['title', 'slug', 'iconfont'], 'string', 'max' => 20],
            [['description'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', 'ID'),
            'parent_id' => Yii::t('common', '父目录ID'),
            'title' => Yii::t('common', '目录名'),
            'slug' => Yii::t('common', '路由(Slug)'),
            'description' => Yii::t('common', '目录介绍'),
            'iconfont' => Yii::t('common', '字体图标'), 
            'enabled' => Yii::t('common', '是否显示'),
            'created_at' => Yii::t('common', '创建时间'),
            'updated_at' => Yii::t('common', '更新时间'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getNotes()
    {
        return $this->hasMany(Note::className(), ['cid' => 'id'])->inverseOf('c');
    }

    /**
     * 设置自动更新时间   created_at | updated_at 字段类型为 int(11) 
     */
    public function behaviors() {
        return [
            \yii\behaviors\TimestampBehavior::className(),
//            // 生成sitemap
//            'sitemap' => [
//                'class' => SitemapBehavior::className(),
//                'scope' => function ($model) {
//                    $model->select(['slug', 'updated_at'])->orderBy('id desc');
//                },
//                'dataClosure' => function ($model) {
//                    /** @var self $model */
//                    return [
//                        'loc' => Url::to('/catelog/'.$model->slug, true),
//                        'lastmod' => strtotime(date('Y/m/d h:i:s', $model->updated_at)),
//                        'changefreq' => SitemapBehavior::CHANGEFREQ_DAILY,
//                        'priority' => 0.6
//                    ];
//                }
//            ],            
        ];
    }    
}
